
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  Registration;

import java.io.RandomAccessFile;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Hashtable;

import logger.PoolLogger;

import User.Uservalidation;


import control.Control;

import dbmanager.ChekInMaster;
import dbmanager.DBManager;
import event.Itemcodegenerator;

public class RegisterItemList 
{
	String Path;
	PoolLogger pl;
	
	public RegisterItemList(String Path) 
	{	
		this.Path = Path;
		// logger
		pl = (PoolLogger) PoolLogger.getLogger(this.getClass().getName());
	}
	
	public int Register(int PropertyCol)
	{
		int Count=0;
		String HOME = "//home//server//Grocessary//";
		try 
		{
			RandomAccessFile ItemOut = new RandomAccessFile(HOME+"ItemOutPut.csv","rws");
			RandomAccessFile raf = new RandomAccessFile(HOME+Path,"rws");
			RandomAccessFile log = new RandomAccessFile(HOME+"Request.txt","rws");
			RandomAccessFile Error = new RandomAccessFile(HOME+"Error.txt","rws");
			Error.seek(Error.length());
			log.seek(log.length());
			String line = null;
			line=raf.readLine();
			String[] col = line.split("\t");
			if(ItemOut.length()==0)
				ItemOut.writeBytes(line+"\tItemcode\n");
			else
				ItemOut.seek(ItemOut.length());
		/*	String col[] = new String[7]; 
			col[0] = "Type";
			col[1] = records[1];  
			col[2] = records[2];
			col[3] = records[3];
			col[4] = records[4];
			col[5] = records[5];
			col[6] = records[6];
			*/
			ResultSet rs = DBManager.getSelect("SELECT * FROM command where fieldname='name' and msgtype = 'master_table' and process='add'");
			rs.next();
			String NameChar = rs.getString("spchar");
			
			rs = DBManager.getSelect("SELECT * FROM command where fieldname='pid' and msgtype = 'property_details' and process='add'");
			rs.next();
			String pidChar = rs.getString("spchar");
			
			rs = DBManager.getSelect("SELECT * FROM command where fieldname='pv' and msgtype = 'property_details' and process='add'");
			rs.next();
			String pvChar = rs.getString("spchar");
			String userid = "+911234567891";
			Uservalidation getpass = new Uservalidation();
			String password = getpass.getPasword(userid);
			String splitline[] = line.split("\t");
			String request = "11 property_details add" +NameChar;
			ChekInMaster checkinmaster = new ChekInMaster();
			
			for(int i=0;i<PropertyCol;i++)
			{
				if(!checkinmaster.isInMaster(splitline[i]))
				{
					String mastermsg =  request + "\"" + splitline[i] + "\"" + pidChar + "type" + pvChar + "other"+"#" + password;
					Control controlClassObj = new Control(userid,mastermsg);
					controlClassObj.messageProcessing();
					controlClassObj.requestProcess();
				}
			}
			while((line=raf.readLine())!=null)
			{
				String message ="";
				String records[] = null;
				try 
				{
					records = line.split("\t");
					String mastername = records[0].trim();
					try
					{
						ResultSet rs1 = DBManager.getSelect("select * from master_table where name like'%"+mastername+"%'");
						int countCheck =0;
						while(rs1.next())
							countCheck++;
						if(countCheck>0)
						mastername = mastername+"_"+countCheck;
						records[0] = mastername;
						
					} catch (Exception e1) 
					{
						e1.printStackTrace();
					}
					message = NameChar+"\""+mastername+"\"";
					String propString ="";
					propString = propString+pidChar+"\"type\""+pvChar+"\"item\"";
					for(int i=1;i<PropertyCol;i++)
					{
						try
						{
							String pv = records[i].trim();
							if(pv.length()!=0)
								propString = propString+pidChar+"\""+col[i].trim()+"\""+pvChar+"\""+pv+"\"";
						}
						catch (Exception e) 
						{
							//propString = propString+pidChar+"\""+col[i].trim()+"\""+pvChar+"\""+"no$"+"\"";
						}
					}
					System.out.println(propString);
					pl.info(propString);
					pl.debug(propString);
					pl.warn(propString);
					pl.fatal(propString);
					pl.error(propString);
					pl.trace(propString);
					message = message + propString;
					System.out.println(message);
					pl.info(message);
					pl.debug(message);
					pl.warn(message);
					pl.fatal(message);
					pl.error(message);
					pl.trace(message);
					message = "11 master_table add"+message+"#"+password;
					log.writeBytes(message+"\n");
					System.out.println(message);
					pl.info(message);
					pl.debug(message);
					pl.warn(message);
					pl.fatal(message);
					pl.error(message);
					pl.trace(message);
					Control controlClassObj = new Control(userid,message);
					controlClassObj.messageProcessing();
					String error = controlClassObj.requestProcess().toString();
					if(error.toLowerCase().contains("error")==false)
					{						
			/*			Itemcodegenerator itemcode = new Itemcodegenerator();
						itemcode.SetData(message,"", "", userid);
						itemcode.initializationData(db);
						itemcode.setObject("");
						System.out.println("Result is : "+itemcode.getResult());
						ItemOut.writeBytes(mastername+"\t");
						for(int i=1;i<records.length;i++)
						{
							ItemOut.writeBytes(records[i]+"\t");
						}
						ItemOut.writeBytes(itemcode.getItemCode()+"\n");
								*/
				
						Count++;
					}
					else
						Error.writeBytes(message+"\n");
					
				} 
				catch (Exception e)
				{
					Error.writeBytes(message+"\n");
				}
			}
			log.close();
			Error.close();
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return Count;
	}
	
	public static void main(String[] args) 
	{
		RegisterItemList reg = new RegisterItemList("grocessary.csv");
		System.out.println("Result :: "+reg.Register(7)+" Items are Registered in the Database");
	}
}
